package com.luo.backend.controller;

import com.luo.backend.common.ApiResponse;
import com.luo.backend.dto.CategoryResponse;
import com.luo.backend.dto.FoodResponse;
import com.luo.backend.dto.MerchantResponse;
import com.luo.backend.service.MerchantService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 商家商品控制器
 */
@RestController
@RequestMapping("/api/v1")
public class MerchantController {
    
    private static final Logger logger = LoggerFactory.getLogger(MerchantController.class);
    
    @Autowired
    private MerchantService merchantService;
    
    /**
     * 获取商家列表
     * 
     * @param page 页码（默认1）
     * @param size 每页数量（默认20）
     * @param status 商家状态（0=休息中，1=营业中）
     * @param sortBy 排序字段（rating/ratingCount/createdAt）
     * @param keyword 搜索关键词
     */
    @GetMapping("/merchants")
    public ApiResponse<Map<String, Object>> getMerchants(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "20") int size,
            @RequestParam(required = false) Integer status,
            @RequestParam(required = false) String sortBy,
            @RequestParam(required = false) String keyword) {
        try {
            Map<String, Object> result = merchantService.getMerchants(page, size, keyword, sortBy, status);
            return ApiResponse.success(result);
        } catch (Exception e) {
            logger.error("获取商家列表失败", e);
            return ApiResponse.error(e.getMessage());
        }
    }
    
    /**
     * 获取商家详情
     */
    @GetMapping("/merchants/{id}")
    public ApiResponse<MerchantResponse> getMerchantById(@PathVariable Long id) {
        try {
            MerchantResponse merchant = merchantService.getMerchantById(id);
            return ApiResponse.success(merchant);
        } catch (Exception e) {
            logger.error("获取商家详情失败", e);
            return ApiResponse.error(e.getMessage());
        }
    }
    
    /**
     * 获取商家商品列表
     */
    @GetMapping("/merchants/{merchantId}/foods")
    public ApiResponse<List<FoodResponse>> getMerchantFoods(
            @PathVariable Long merchantId,
            @RequestParam(required = false) Long categoryId,
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) Boolean isHot,
            @RequestParam(required = false) Boolean isRecommended) {
        try {
            List<FoodResponse> foods = merchantService.getMerchantFoods(
                merchantId, categoryId, keyword, isHot, isRecommended);
            return ApiResponse.success(foods);
        } catch (Exception e) {
            logger.error("获取商家商品列表失败", e);
            return ApiResponse.error(e.getMessage());
        }
    }
    
    /**
     * 获取商品详情
     */
    @GetMapping("/foods/{id}")
    public ApiResponse<FoodResponse> getFoodById(@PathVariable Long id) {
        try {
            FoodResponse food = merchantService.getFoodById(id);
            return ApiResponse.success(food);
        } catch (Exception e) {
            logger.error("获取商品详情失败", e);
            return ApiResponse.error(e.getMessage());
        }
    }
    
    /**
     * 搜索商品
     */
    @GetMapping("/foods/search")
    public ApiResponse<Map<String, Object>> searchFoods(
            @RequestParam String keyword,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "20") int limit) {
        try {
            Map<String, Object> result = merchantService.searchFoods(keyword, page, limit);
            return ApiResponse.success(result);
        } catch (Exception e) {
            logger.error("搜索商品失败", e);
            return ApiResponse.error(e.getMessage());
        }
    }
    
    /**
     * 获取商品分类
     */
    @GetMapping("/food-categories")
    public ApiResponse<List<CategoryResponse>> getFoodCategories() {
        try {
            List<CategoryResponse> categories = merchantService.getFoodCategories();
            return ApiResponse.success(categories);
        } catch (Exception e) {
            logger.error("获取商品分类失败", e);
            return ApiResponse.error(e.getMessage());
        }
    }
}
